package io.github.lee.jel.ui.item.list

import android.graphics.Color
import android.util.TypedValue
import androidx.appcompat.widget.Toolbar
import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fondesa.recyclerviewdivider.dividerBuilder
import dagger.hilt.android.AndroidEntryPoint
import io.github.lee.core.ui.BaseRefreshFragment
import io.github.lee.core.ui.databinding.ToolbarSimpleCenterTitleBinding
import io.github.lee.core.util.getArgumentParcelable
import io.github.lee.core.util.getColor
import io.github.lee.core.util.toPx
import io.github.lee.jel.R
import io.github.lee.jel.domain.Item
import io.github.lee.jel.ui.item.detail.ItemDetailActivity
import io.github.lee.jel.utils.screenWidthDP

@AndroidEntryPoint
class ItemListFragment : BaseRefreshFragment<ItemListVM>() {

    companion object {


        private const val KEY_ITEM = "ITEM"
        fun newInstance(
            item: Item
        ): ItemListFragment {
            val args = bundleOf(
                KEY_ITEM to item,
            )
            val fragment = ItemListFragment()
            fragment.arguments = args
            return fragment
        }
    }
    //===Desc:=============================================================

    override fun onCreateViewModel(): ItemListVM =
        viewModels<ItemListVM>().value.also {
            val parentItem = getArgumentParcelable(KEY_ITEM, Item::class.java)
            if (null == parentItem) {
                toast(getString(R.string.error_application_error))
                activity?.finish()
                return@also
            }
            it.parentItem = parentItem
        }

    override fun onCreateLayoutManager(): RecyclerView.LayoutManager =
        GridLayoutManager(mContext, if (screenWidthDP(mContext) > 600) 7 else 3)
            .apply {
                spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                    override fun getSpanSize(position: Int): Int {

                        return if (position == ((mAdapterHelper?.adapter?.adapters?.size
                                ?: 0) + (vm?.adapter?.items?.size ?: 0) - 1) - 1
                        ) {
                            spanCount
                        } else {
                            1
                        }
                    }

                }
            }
    //===Desc:=============================================================

    override fun onCreateToolbar(): Toolbar =
        ToolbarSimpleCenterTitleBinding.inflate(layoutInflater)
            .apply {
                bg = getColor(R.color.main_background_grey)
                title = getArgumentParcelable(KEY_ITEM, Item::class.java)?.name
                titleColor = getColor(R.color.main_black)
            }
            .toolbar
    //===Desc:=============================================================


    override fun onSetViewListener() {
        super.onSetViewListener()
        vm?.adapter?.setOnItemClickListener { _, _, position ->
            val item = vm?.getItem(position)
            if (null == item) {
                toast(getString(R.string.error_application_error))
                return@setOnItemClickListener
            }
            ItemDetailActivity.start(mContext, item)
        }

    }

    override fun onSetViewData() {
        super.onSetViewData()
        statusBarColor(getColor(R.color.main_background_grey), true)
        setNavigationIcon(
            R.drawable.outline_arrow_back_ios_new_24,
            getColor(R.color.main)
        ) { activity?.finish() }

        recyclerView?.setBackgroundColor(getColor(R.color.main_background_grey))
        val vp = 8.toPx(mContext)
        val hp = 16.toPx(mContext)
        recyclerView?.setPadding(hp, vp, hp, vp)
        addItemDecoration(
            mContext.dividerBuilder()
                .color(Color.TRANSPARENT)
                .size(8, TypedValue.COMPLEX_UNIT_DIP)
                .build()
        )

    }
}